package com.artfii.fluentsql.core.tools;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;


import java.util.Date;
import java.util.logging.Logger;


public class DateUtils {
    private static Logger logger = Logger.getLogger(DateUtils.class.getName());

    private static DateTimeFormatter formatter;
    static {
        DateTimeParser[] parsers = {
                DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").getParser(),
                DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS").getParser(),
                DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSZ").getParser(),
                DateTimeFormat.forPattern("yyyyMMdd").getParser(),
                DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
                DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").getParser(),
                DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").getParser(),
                DateTimeFormat.forPattern("yyyy/MM/dd").getParser(),
                DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss").getParser(),
                DateTimeFormat.forPattern("yyyy/MM/dd'T'HH:mm:ss").getParser(),
                DateTimeFormat.forPattern("yyyy/MM/dd'T'HH:mm:ss.SSS").getParser(),
                DateTimeFormat.forPattern("yyyy/MM/dd'T'HH:mm:ss.SSSZ").getParser(),
                DateTimeFormat.forPattern("yyyyMMddHHmmss").getParser(),
                DateTimeFormat.forPattern("yyyyMMddHHmmssZ").getParser(),
                DateTimeFormat.forPattern("EEE, d MMM yyyy HH:mm:ss Z").getParser(),
                DateTimeFormat.forPattern("MMM d, yyyy HH:mm:ss a").getParser(),
                DateTimeFormat.forPattern("EEE, MMM d, ''yy").getParser(),
                //在此增加新的日期 pattern
        };
        formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
    }

    /**
     * 把常见的日期字串转日期
     *
     * @param dateStr
     * @return
     * @author:lqf
     */
    public static DateTime toJodaDate(String dateStr) {
        if (StringKit.isEmpty(dateStr)) return null;
        if ("0000-00-00 00:00:00".equals(dateStr)) return null;
        if (isPhpTime(dateStr)) {
            return new DateTime(fixFastTime(dateStr));
        }
        try {
            return formatter.parseDateTime(dateStr);
        } catch (IllegalArgumentException e) {
            logger.warning("日期解析出错:"+dateStr);
        }
        return null;
    }

    public static Date toDate(String dateStr) {
        return toJodaDate(dateStr).toDate();
    }

    private static boolean isPhpTime(String dateStr){
        return StringKit.isNumber(dateStr) && 10 <= dateStr.length();
    }

    private static long fixFastTime(String dateStr) {//补足13位
        StringBuffer d = new StringBuffer(13).append(dateStr);
        while (d.length()<13){
            d.append("0");
        }
        return Long.parseLong(d.toString());
    }

    public static void main(String[] args) {
        String d = "1686279395";
        Date d1 = DateUtils.toDate(d);
        logger.info(d1.toString());
    }
}
